<template>
  <el-card >
    <div slot="header" class="clearfix">
      <my-breadcrumb>编辑文章</my-breadcrumb>
    </div>
    <el-form label-width="80px" ref="myForm" :rules="rules" :model="form">
      <el-form-item label="标题" prop="title">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <quill-editor v-model="form.content" :options="editorOption"></quill-editor>
      </el-form-item>
      <el-form-item label="封面">
        <el-radio-group v-model="coverType"  @change="hImageTypeChange">
          <el-radio :label="1">单图</el-radio>
          <el-radio :label="3">三图</el-radio>
          <el-radio :label="0">无图</el-radio>
          <el-radio :label="-1">自动</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="coverType > 0">
      <el-row>
        <el-col :span="6" v-for="(item, idx) in coverType" :key="item.id">
           <my-cover v-model="coverImages[idx]"></my-cover>
        </el-col>
      </el-row>
      </el-form-item>
      <my-channel v-model="form.channelId"></my-channel>
      <el-form-item>
        <el-button type="primary" @click="hPublish" :loading="isloading">保存更改</el-button>
      </el-form-item>
    </el-form>

  </el-card>
</template>

<script>
// 导入样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// 导入组件
import { quillEditor } from 'vue-quill-editor'
export default {
  components: {
    quillEditor
  },
  name: 'EditArticle',
  data () {
    return {
      isloading: false,
      coverType: '',
      coverImages: [], // 保存对应的封面图的地址
      form: {
        title: '',
        content: '',
        channelId: ''
      },
      editorOption: { // quillEditor的配置项，用来自定义工具栏
        // 占位配置
        placeholder: '',
        modules: {
          // 配置工具栏
          toolbar: [
            ['bold', 'italic'],
            ['blockquote', 'code-block'],
            [{ header: 1 }, { header: 2 }],
            [{ list: 'ordered' }, { list: 'bullet' }],
            [{ indent: '-1' }, { indent: '+1' }],
            ['image']
          ]
        }
      },
      // 自定义规则
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' },
          { min: 5, max: 30, message: '长度在5到30之间', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入文章内容', trigger: 'blur' },
          { min: 20, max: 30000, message: '最少20个字', trigger: 'blur' }
        ],
        channelId: [
          { required: true, message: '请选择频道', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.loadArticle()
  },
  methods: {
    async doPublish () {
      try {
        const { title, content, channelId } = this.form // 解构赋值
        // 发送请求
        this.isloading = true
        const res = await this.$http({
          method: 'put',
          url: '/mp/v1_0/articles/' + this.$route.params.id,
          // 参数
          data: {
            title,
            content,
            channel_id: channelId,
            cover: {
              type: this.coverType,
              images: this.coverImages
            }
          }
        })
        console.log(res)
        this.$message.success('保存成功')
        this.isloading = false
      } catch (err) {
        console.log(err)
        this.$message.error('保存失败')
      }
      this.isloading = false
    },
    hPublish (draft) {
      this.$refs.myForm.validate(valid => { // 验证是否有效
        if (valid) {
          this.doPublish(draft)
        }
      })
    },
    async loadArticle () {
      try {
        const res = await this.$http({
          url: '/mp/v1_0/articles/' + this.$route.params.id,
          method: 'GET'
        })
        this.form.title = res.data.data.title
        this.form.content = res.data.data.content
        this.form.channelId = res.data.data.channel_id
        this.coverImages = res.data.data.cover.images
        this.coverType = res.data.data.cover.type
      } catch (err) {
        console.log(err)
      }
    },
    hImageTypeChange () {
      if (this.coverType === 1) {
        this.coverImages.length = 1
      } else if (this.coverType === 3) {
        this.coverImages.length = 1
      } else {
        this.coverImages.length = []
      }
    }
  }

}
</script>

<style scoped lang='less'>
// 在父组件中去设置子组件的样式，加上/deep/ 实现穿透功能
 /deep/ .ql-editor{
  height: 300px;
}
 /deep/ .ql-toolbar.ql-snow{
  padding: 0 8px;
}
</style>
